// pages/tuanduilist/index.js
const {
    globalData
} = getApp()
import {
    bookTeamNow
} from "../../api/home"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //正常列表
        infoResultList: '',
        //信息不全集合
        emptyUsers: "",
        //健康码异常
        errorCodes: "",
        teamName: '',
        travelDate: '',
        dayType: '',
        sessionTime: '',
        teamLeader: '',
        faceUrl: '',
        teamCommitmentUrl: "",
        teamLimitMax: "",
        teamLimitMin: "",
        repeats:''
    },
    //返回上一页
    onClickLeft() {
        wx.navigateBack({
            delta: 1,
        })
    },
    // 获取滚动条当前位置
    onPageScroll: function (e) {
        // console.log(e)
        if (e.scrollTop > 100) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },
      //回到顶部
      goTop: function (e) { // 一键回到顶部
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },
    //提交表单 运行yy
    // 提交 英文 submit ？？？？？？？？？？？？我
    oksunmit() {
        let arr  = this.data.infoResultList.map(item=>{
            return item.cardNum
        })
        // console.log(arr)
        for (var i = 0; i < arr.length; i++) {
            console.log(arr.indexOf(arr[i]));
            if(arr.indexOf(arr[i]) != i) {
                // console.log('重复元素',arr[i])
                wx.showModal({
                    title:"温馨提示",
                    content: '有重复身份证号，请修改',
                })
                return;
            }
        }
        //   console.log(this.data.dayType,"上/下午");
        //   console.log(this.data.faceUrl,"团队人脸");
        //   console.log(this.data.infoResultList.length,"团队人数");
        //   console.log(this.data.sessionTime,'入场时间');
        //   console.log(this.data.teamCardNum,'领队人身份证');
        //   console.log(this.data.teamCommitmentUrl,"安全承诺");
        //   console.log(this.data.teamLeader,'负责人姓名');
        //   console.log(this.data.teamName,"团队名称");
        //   console.log(this.data.teamPhone ,"负责人手机号");
        //   console.log(this.data.travelDate ,"出现日期");
        //   console.log(this.data.infoResultList,"我是团队预约游客信息集合");
        const flag = this.data.infoResultList.every(item => {
            const nameFlag = item.name ? true : false
            const cardFlag = item.cardNum ? true : false
            //   const isPhone = item.phone ? true : false
            const healthCodeFlag = item.healthCode === '绿码' ? true : false
            return nameFlag && cardFlag && healthCodeFlag
        })
        // console.log(flag);
        //   return
        if (this.data.infoResultList.length + 1 < this.data.teamLimitMin) {
            wx.showModal({
                title: '温馨提示',
                content: '团队预约人数最少' + this.data.teamLimitMin + '人'
            })
        } else if (this.data.infoResultList.length > this.data.teamLimitMax) {
            wx.showModal({
                title: '温馨提示',
                content: '团队预约人数最多' + this.data.teamLimitMax + '人'
            })
        } else if (flag == true) {
            bookTeamNow({
                dayType: this.data.dayType,
                faceUrl: this.data.faceUrl,
                infoResultList: this.data.infoResultList,
                num: this.data.infoResultList.length + 1,
                sessionTime: this.data.sessionTime,
                teamCardNum: this.data.teamCardNum,
                teamCommitmentUrl: this.data.teamCommitmentUrl,
                teamLeader: this.data.teamLeader,
                teamName: this.data.teamName,
                teamPhone: this.data.teamPhone,
                travelDate: this.data.travelDate
            }).then((res) => {
                console.log(res);
                if (res.code == 1000) {
                    wx.showLoading({
                        title: '加载中~',
                    })
                    wx.navigateTo({
                        url: '/pages/tuanduiyuyueok/index?id=' + res.data.id,
                    })
                } else {
                    wx.showModal({
                        title: res.data.msg,
                    })
                }
            })
        } else {
            wx.showToast({
                title: "用户信息不完整",
                icon: "error"
            })
        }

    },
  
    //点击编辑
    bianji(e) {
        let index = e.currentTarget.dataset.index
        globalData.currentIndex = index
        console.log(this.data.infoResultList[index]);
        wx.navigateTo({
            url: '/pages/bianji/index?name=' + this.data.infoResultList[index].name + '&cardNum=' + this.data.infoResultList[index].cardNum + '&phone=' + this.data.infoResultList[index].phone + "&id="  + this.data.infoResultList[index].id + '&type=' + 1,
        })
    },
    //点击删除
    deleimage(e) {
        console.log(e);
        const {
            infoResultList
        } = this.data
        let index = e.currentTarget.dataset.index
        console.log(this.data.infoResultList[index]);
        console.log(index);
        wx.showModal({
            title: "确定要删除用户" + this.data.infoResultList[index].name,
            success: (res) => {
                if (res.confirm) {
                    console.log('用户点击确定')
                    this.data.infoResultList.splice(index, 1)
                    this.setData({
                        infoResultList
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            teamLimitMin: wx.getStorageSync('teamLimitMin'),
            teamLimitMax: wx.getStorageSync('teamLimitMax')
        })
        wx.showLoading({
            title: '加载中',
        })
        console.log(options);
        // console.log(JSON.parse(options.infoResultList));
        console.log(options.dayType, "上午/下午");
        console.log(options.faceUrl, '团队领队这头像');
        console.log(options.sessionTime, '入场时间');
        console.log(options.teamCardNum, "我是团队预约人身份证号");
        console.log(options.arrsrc, "安全");
        console.log(options.teamLeader, "负责人姓名");
        console.log(options.teamName, '团队名称');
        console.log(options.teamPhone, "负责人手机号");
        console.log(options.travelDate, "出行日期");
        // console.log(JSON.parse(options.emptyUsers));
        // console.log(JSON.parse(options.errorCodes));
        this.setData({
            teamCardNum: options.teamCardNum,
            faceUrl: options.faceUrl == "" ? '1' : options.faceUrl,
            teamCommitmentUrl: options.arrsrc,
            teamPhone: options.teamPhone,
            teamLeader: options.teamLeader,
            dayType: options.dayType,
            sessionTime: options.sessionTime,
            travelDate: options.travelDate,
            teamName: options.teamName,
            infoResultList: JSON.parse(options.infoResultList),
            emptyUsers: JSON.parse(options.emptyUsers),
            errorCodes: JSON.parse(options.errorCodes),
            repeats:JSON.parse(options.repeats)
        })
        wx.hideLoading({})
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        const {
            currentIndex,
            currentItem,
            idzhengjian
        } = globalData
        globalData.idzhengjian=this.data.infoResultList
        console.log(idzhengjian);
        if (currentIndex !== -1) {
            this.setData({
                //小程序局部刷新
                //列表             下标               全局定义
                [`infoResultList[${currentIndex}]`]: currentItem
            })
            globalData.currentIndex = -1
            globalData.currentItem = {}
            
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})